package com.jianbing.shasha.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jianbing.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jianbing.common.annotation.Log;
import com.jianbing.common.core.controller.BaseController;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.enums.BusinessType;
import com.jianbing.shasha.domain.ShashaUserInfo;
import com.jianbing.shasha.service.IShashaUserInfoService;
import com.jianbing.common.utils.poi.ExcelUtil;
import com.jianbing.common.core.page.TableDataInfo;

/**
 * shasha项目用户基本信息Controller
 * 
 * @author ruoyi
 * @date 2024-06-09
 */
@RestController
@RequestMapping("/shasha/info")
public class ShashaUserInfoController extends BaseController
{
    @Autowired
    private IShashaUserInfoService shashaUserInfoService;

    /**
     * 查询shasha项目用户基本信息列表
     */
    // @PreAuthorize("@ss.hasPermi('shasha:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShashaUserInfo shashaUserInfo)
    {
        startPage();
        List<ShashaUserInfo> list = shashaUserInfoService.selectShashaUserInfoList(shashaUserInfo);
        return getDataTable(list);
    }

    /**
     * 导出shasha项目用户基本信息列表
     */
    // @PreAuthorize("@ss.hasPermi('shasha:info:export')")
    @Log(title = "shasha项目用户基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShashaUserInfo shashaUserInfo)
    {
        List<ShashaUserInfo> list = shashaUserInfoService.selectShashaUserInfoList(shashaUserInfo);
        ExcelUtil<ShashaUserInfo> util = new ExcelUtil<ShashaUserInfo>(ShashaUserInfo.class);
        util.exportExcel(response, list, "shasha项目用户基本信息数据");
    }

    /**
     * 获取shasha项目用户基本信息详细信息
     */
    //   @PreAuthorize("@ss.hasPermi('shasha:info:query')")
    @GetMapping
    public AjaxResult getInfo()
    {
        Long userId = SecurityUtils.getUserId();
        return success(shashaUserInfoService.selectShashaUserInfoByUserId(userId));
    }

    /**
     * 新增shasha项目用户基本信息
     */
    //   @PreAuthorize("@ss.hasPermi('shasha:info:add')")
    @Log(title = "shasha项目用户基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShashaUserInfo shashaUserInfo)
    {
        Long userId = SecurityUtils.getUserId();
        if(shashaUserInfoService.selectShashaUserInfoByUserId(userId)!=null){
            return AjaxResult.error("用户信息已经插入 使用更新而不是插入");
        }
        shashaUserInfo.setUserId(userId);
        shashaUserInfo.setLevelProcessId(1L);
        shashaUserInfo.setRemainingLevels(2L);
        shashaUserInfo.setInsertTime(new Date());
        return toAjax(shashaUserInfoService.insertShashaUserInfo(shashaUserInfo));
    }

    /**
     * 修改shasha项目用户基本信息
     */
    //   @PreAuthorize("@ss.hasPermi('shasha:info:edit')")
    @Log(title = "shasha项目用户基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ShashaUserInfo shashaUserInfo)
    {
        Long userId = SecurityUtils.getUserId();
        if(shashaUserInfoService.selectShashaUserInfoByUserId(userId)==null){
            return AjaxResult.error("用户信息还没插入");
        }
        shashaUserInfo.setUserId(userId);
        shashaUserInfo.setLevelProcessId(null);
        shashaUserInfo.setRemainingLevels(null);
        shashaUserInfo.setInsertTime(null);
        return toAjax(shashaUserInfoService.updateShashaUserInfo(shashaUserInfo));
    }

    /**
     * 删除shasha项目用户基本信息
     */
    //  @PreAuthorize("@ss.hasPermi('shasha:info:remove')")
    @Log(title = "shasha项目用户基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(shashaUserInfoService.deleteShashaUserInfoByUserIds(userIds));
    }

    /**
     * 获取用户当前挑战进度
     */
    //  @PreAuthorize("@ss.hasPermi('shasha:info:process')")
    @Log(title = "shasha项目用户基本信息", businessType = BusinessType.DELETE)
    @GetMapping("/process")
    public AjaxResult process()
    {
        Long userId = SecurityUtils.getUserId();
        return success(shashaUserInfoService.getProcessByUserId(userId));
    }

    /**
     * 修改shasha项目用户挑战进度
     */
    //  @PreAuthorize("@ss.hasPermi('shasha:info:editProcess')")
    @Log(title = "shasha项目用户基本信息", businessType = BusinessType.UPDATE)
    @PutMapping("/process")
    public AjaxResult editProcess(@RequestBody ShashaUserInfo shashaUserInfo)
    {
        Long userId = SecurityUtils.getUserId();
        if(shashaUserInfoService.selectShashaUserInfoByUserId(userId)==null){
            return AjaxResult.error("用户信息还没插入");
        }
        //保证只能修改remaining level
        ShashaUserInfo shashaUserInfo1 = new ShashaUserInfo();
        shashaUserInfo1.setUserId(userId);
        shashaUserInfo1.setRemainingLevels(shashaUserInfo.getRemainingLevels());
        return toAjax(shashaUserInfoService.updateShashaUserInfo(shashaUserInfo1));
    }
}
